import registnet.dao.GradePeer;
import registnet.dao.Grade;

import registnet.records.Transcript;
import registnet.records.source.TranscriptSource;
import registnet.records.CourseTaken;
import registnet.records.Student;
import registnet.records.source.StudentSource;

import resources.Constants;

/* [start] 	initializes gui variables */
String _actions = bundle.getProperty("registnet.module.student_records.macro.transcript.form.actions");
String _export = bundle.getProperty("registnet.module.student_records.macro.transcript.form.actions.export");
String _print = bundle.getProperty("registnet.module.student_records.macro.transcript.form.actions.print");

String _year_semester = bundle.getProperty("registnet.module.student_records.macro.transcript.form.year_semester");
String _total_units = bundle.getProperty("registnet.module.student_records.macro.transcript.form.total_units");
String _course_code = bundle.getProperty("registnet.module.student_records.macro.transcript.form.course_code");
String _course_title = bundle.getProperty("registnet.module.student_records.macro.transcript.form.course_title");
String _section = bundle.getProperty("registnet.module.student_records.macro.transcript.form.section");
String _units = bundle.getProperty("registnet.module.student_records.macro.transcript.form.units");
String _credit = bundle.getProperty("registnet.module.student_records.macro.transcript.form.credit");
String _grade = bundle.getProperty("registnet.module.student_records.macro.transcript.form.grade");
String _reexam = bundle.getProperty("registnet.module.student_records.macro.transcript.form.reexam");
String _remarks = bundle.getProperty("registnet.module.student_records.macro.transcript.form.remarks");
/* [end] */


/* [start]	initializes the year-semester hashmap */
List grades = new GradePeer().getGradeByStudent(DAOStudent.getStudentId());
List ascendingKey = new Vector();
HashMap yearSemester = new HashMap();
for(int i=0; i<grades.size(); i++){
	Grade grade = grades.get(i);
	int[] y_s = new int[2];
	y_s[0] = grade.getSemester();
	y_s[1] = grade.getAcademicYear();
	String key;
	if (y_s[0] == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
		key = (y_s[1] - 1) +"-"+ y_s[1] +":"+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	else
		key = y_s[1] +"-"+ (y_s[1] + 1) +":"+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	if (!yearSemester.containsKey(key)){
		yearSemester.put(key, y_s);
		ascendingKey.add(key);
	}
}
/* [end] */


Student student = new StudentSource(DAOStudent.getStudentNumber()).getStudent();


/* [start]	defines a renderer for the contents of the transcript */
private class TranscriptRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		if (data[1] != null){
			new Label(data[0]).setParent(row);
			new Label(data[1]).setParent(row);
			new Label(data[2]).setParent(row);
			new Label(data[3]).setParent(row);
			new Label(data[4]).setParent(row);
			new Label(data[5]).setParent(row);
			new Label(data[6]).setParent(row);
			new Label(data[7]).setParent(row);
		}
		else if(data[0].equals("")){
			Label label = new Label(data[0]);
			label.setParent(row);
			row.setSpans("8");
			row.setHeight("25px");
			row.setStyle("border-top: 2px solid #4f7fbf; background-color: #ffffff; ");
		}
		else{
			Label label = new Label(data[0]);
			label.setStyle("font-weight: bold");
			label.setParent(row);
			row.setSpans("8");
			row.setStyle("border-top: 2px solid #4f7fbf; border-bottom: 2px solid #4f7fbf; background-color: #BCD2EF;");
		}
	}
}
/* [end] */



/* [start]	retrieves the courses on the transcript and places them on the grid */
void retrieveCourses(){
	Transcript transcript = new TranscriptSource(student).getTranscript();
	List courses = transcript.getCourses();
	ListIterator iter = ascendingKey.listIterator();
	String[][] listModel = new String[courses.size() + (2*yearSemester.size())][8];
	int i=0;
	while(iter.hasNext()){
		String key = iter.next();
		int[] y_s = yearSemester.get(key);
		listModel[i][0] = key + " (" + transcript.getUnits(y_s[0], y_s[1]) + " " + _units + ")";
		i++;
		
		List semesterCourses = transcript.getCoursesTaken(y_s[0], y_s[1]);
		ListIterator iterator = semesterCourses.listIterator();
		while(iterator.hasNext()){
			CourseTaken course = iterator.next();
			listModel[i][0] = course.getCourseCode();
			listModel[i][1] = course.getCourseName();
			listModel[i][2] = course.getSection().getSectionName();
			listModel[i][3] = course.getUnit() + "";
			listModel[i][4] = course.getCredit() + "";
			listModel[i][5] = Constants.MAP_GRADE_VALUE.get(course.getGrade());
			if (course.getReExam() == 0)
				listModel[i][6] = "-";
				else
				listModel[i][6] = Constants.MAP_GRADE_VALUE.get(course.getReExam());
			listModel[i][7] = course.getRemarks();
			i++;
		}
		listModel[i][0] ="";
		i++;
	}
	ListModel strset = new SimpleListModel(listModel);
	transcript_g.setModel(strset);
	transcript_g.setRowRenderer(new TranscriptRenderer());	
}
/* [end] */